package cn.com.ddapp.ddm.ui.mine;

import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.Entity;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.SPUtils;

/**
 * Created by Administrator on 2016/8/30.
 */
public class BindAlipayActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    @BindView(R.id.alipayName)
    EditText alipayName;
    @BindView(R.id.alipayAccount)
    EditText alipayAccount;
    @BindView(R.id.error_layout)
    EmptyLayout mEmptyLayout;

    String name,account,userId;

    @Override
    public void initView() {
        titletext.setText("绑定支付宝");
    }

    @Override
    public void initData() {

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_bind_alipay;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

    @OnClick({R.id.back,R.id.bd_alipay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bd_alipay:
                userId = SPUtils.getStringData(this, SPUtils.USERID);
                name = alipayName.getText().toString();
                account = alipayAccount.getText().toString();
                if (!NetUtils.isConnected(this)) {
                    Toast.makeText(this, "请检查你的网络", Toast.LENGTH_SHORT).show();
                } else {
                    mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                    OkUtilCallback callback = new OkUtilCallback(mEmptyLayout) {
                        @Override
                        public void onMyResponse(String response, int id) {
                            Gson gson = new Gson();
                            Entity card = gson.fromJson(response, Entity.class);
                            if ((!AppConfig.RESULT_OK.equalsIgnoreCase(card.getResult())) && card.getLogin()) {
                                BaseApplication.getIntstance().showToast(card.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                            } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(card.getResult()))) {
                                BaseApplication.getIntstance().showToast(card.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                            } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(card.getResult()))) {
                                BaseApplication.getIntstance().showToast(card.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                                finish();
                            }
                        }
                    };
                    //绑定支付宝
                    /*userId int[Y] 会员ID
                    name string[Y] 支付宝姓名
                    account string[Y] 支付宝账号*/
                    OKUtilAPI.post(OKUtilAPI.addAlipay, "userId=" + userId + "&name="+name +"&account="+account, callback);
                }
                break;
            case R.id.back :
                finish();
                break;
        }
    }
}
